package com.android.projetihm.activity.main;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;

import com.android.projetihm.R;
import com.android.projetihm.activity.main.MainFragment.MY_TABS;

/**
 * This is the main activity : home page
 * 
 * @author Adrien
 *
 */
public class MainActivity
		extends Activity
{

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current tab position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar to show tabs.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the application, add a tab to the action bar.
        for (MY_TABS tab : MY_TABS.values())
        {
            actionBar.addTab(actionBar.newTab().setText(tab.getTitleResID())
            		.setTabListener(new MyTabListener(this)));
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        // Restore the previously serialized current tab position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM))
        {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        // Serialize the current tab position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
                getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
//    @Override
//    public void onTabSelected(
//    		ActionBar.Tab tab,
//    		FragmentTransaction fragmentTransaction)
//    {
//        // When the given tab is selected, show the tab contents in the
//        // container view.
//        Fragment fragment = MainFragment.getFragmentFromTabPosition(tab.getPosition());
//        fragmentTransaction
//                .replace(R.id.container, fragment)
//                .commit();
//    }
    
    public static class MyTabListener
    		implements TabListener
    {
    	private Fragment mFragment;
    	private final Activity mActivity;
    	
    	public MyTabListener(Activity activity)
    	{
    		this.mActivity = activity;
    	}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft){}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft)
		{
			// Check if the fragment is already initialized
			if (this.mFragment == null)
			{
				System.out.println("new Fragment");
				// If not, instantiate and add it to the activity
				this.mFragment = Fragment.instantiate(this.mActivity, MainFragment.class.getName());
				this.mFragment.setRetainInstance(true);
				// with good argument
				int tabPosition = tab.getPosition();
	            Bundle args = new Bundle();
	            args.putInt(
	            		MainFragment.CST_ARG_SECTION_NUMBER,
	            		tabPosition);
	            this.mFragment.setArguments(args);
	            
				ft.add(
						android.R.id.content,
						this.mFragment,
						"tabTag" + tabPosition);
			} else {
				// If it exists, simply attach it in order to show it
				ft.attach(this.mFragment);
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft)
		{
			 if (this.mFragment != null)
			 {
				 ft.detach(this.mFragment);
			 }
		}
    }
}
